<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="外键(绘本子项id)" prop="itemId">
      <el-input v-model="dataForm.bookItemId" placeholder="外键(绘本子项id)"></el-input>
    </el-form-item>
    <el-form-item label="外键(所属绘本id)(冗余)" prop="picbookId">
      <el-input v-model="dataForm.bookId" placeholder="外键(所属绘本id)(冗余)"></el-input>
    </el-form-item>
    <el-form-item label="名称(如: 001)" prop="dltName">
      <el-input v-model="dataForm.dltName" placeholder="名称(如: 001)"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="pic">
      <el-input v-model="dataForm.pic" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="音频文件" prop="audio">
      <el-input v-model="dataForm.audio" placeholder="音频文件"></el-input>
    </el-form-item>
    <el-form-item label="字幕文件" prop="subtitle">
      <el-input v-model="dataForm.subtitle" placeholder="字幕文件"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bookItemId: '',
          bookId: '',
          dltName: '',
          pic: '',
          audio: '',
          subtitle: '',
          remark: ''
        },
        dataRule: {
          bookItemId: [
            { required: true, message: '外键(绘本子项id)不能为空', trigger: 'blur' }
          ],
          bookId: [
            { required: true, message: '外键(所属绘本id)(冗余)不能为空', trigger: 'blur' }
          ],
          dltName: [
            { required: true, message: '名称(如: 001)不能为空', trigger: 'blur' }
          ],
          pic: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ],
          audio: [
            { required: true, message: '音频文件不能为空', trigger: 'blur' }
          ],
          subtitle: [
            { required: true, message: '字幕文件不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/picbookitemdtl/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.pbBookItemDetail
                // this.dataForm.itemId = data.picbookitemdtl.itemId
                // this.dataForm.picbookId = data.picbookitemdtl.picbookId
                // this.dataForm.dltName = data.picbookitemdtl.dltName
                // this.dataForm.pic = data.picbookitemdtl.pic
                // this.dataForm.audio = data.picbookitemdtl.audio
                // this.dataForm.subtitle = data.picbookitemdtl.subtitle
                // this.dataForm.remark = data.picbookitemdtl.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/picbookitemdtl/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                pbBookItemDetail: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
